'use strict';

/**
 * Default test server configuration.
 */
const webpack = require('webpack');
const WebpackBaseConfig = require('./Base');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HappyPack = require('happypack');
const os = require('os');
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });
const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const FriendlyErrors = require('friendly-errors-webpack-plugin');
class WebpackDevelopConfig extends WebpackBaseConfig {

  constructor() {
    super();
    this.config = {
      mode: "development",
      devtool: 'cheap-module-eval-source-map',
      entry: {
        app: [
          'react-hot-loader/patch',
          'webpack-dev-server/client?http://localhost:8000/',
          'webpack/hot/only-dev-server',
          './client.js'
        ],
      },
      devServer: {
        contentBase: './src/',
        publicPath: '/assets/',
        historyApiFallback: true,
        hot: true,
        inline: true,
        quiet: true,  // 隐藏打包日志
        port: 8050,
        disableHostCheck: true,
        proxy: [{
          context: ['/multilanguage'],
          target: 'https://cloudhelios-static.oss-cn-shanghai.aliyuncs.com',
          secure: false,
          changeOrigin: true
        }]
      },
      optimization: {
        splitChunks: {
          hidePathInfo: false,
          minSize: 10000,
          maxAsyncRequests: Infinity,
          maxInitialRequests: Infinity,
          cacheGroups: {
            vendor: { // 基本框架
              name: 'vendor',
              chunks: 'initial',
              test: /node_modules\//,
            }
          }
        },
      },
      plugins: [
        new HappyPack({
          id: 'happyBabel',
          loaders: [{
            loader: 'babel-loader',
            options: {
              cacheDirectory: true
            },
          }],
          threadPool: happyThreadPool,
          verbose: true,
        }),
        new HappyPack({
          id: 'happyScss',
          loaders: ['style-loader','css-loader', 'sass-loader', {
            loader: 'sass-resources-loader',
            options: {
              resources: path.resolve(__dirname, '../../src/styles/themes/color/default.scss'),
            }
          }],
          threadPool: happyThreadPool,
          verbose: true,
        }),
        new HappyPack({
          id: 'happyLess',
          loaders: ['style-loader','css-loader', { loader: 'less-loader', options: { javascriptEnabled: true }}],
          threadPool: happyThreadPool,
          verbose: true,
        }),
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
          filename: '../index.html',
          template: 'index.html',
          inject: true,
        }),
        new FriendlyErrors(),

        // 是否开启分析
        // new BundleAnalyzerPlugin(),
      ]
    };
  }

  get env() {
    return 'develop';
  }
}

module.exports = WebpackDevelopConfig;
